package com.gardenguell.salad.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.gardenguell.salad.R;
import com.gardenguell.salad.app.AppConstant;
import com.gardenguell.salad.data.client.SaladClient;
import com.gardenguell.salad.data.model.Token;
import com.gardenguell.salad.data.volley.OnErrorResponse;
import com.gardenguell.salad.data.volley.OnSuccessResponse;
import com.gardenguell.salad.ui.activity.base.BaseActivity;
import com.gardenguell.salad.util.SharedPreferenceUtil;

/**
 * Created by absurd on 2015/11/14.
 */
public class LoginActivity extends BaseActivity{


    private TextView userNameTv;
    private TextView passwordTv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        userNameTv = (TextView)findViewById(R.id.tv_first);
        passwordTv = (TextView)findViewById(R.id.tv_second);

        userNameTv.setHint(R.string.input_username);
        passwordTv.setHint(R.string.input_password);

        userNameTv.setText("test");
        passwordTv.setText("test");
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch(v.getId()){
            case R.id.btn_login:
                SaladClient saladClient = new SaladClient();
                final String username = userNameTv.getText().toString().trim();
                final String password = passwordTv.getText().toString().trim();
                saladClient.login(username, password, new OnSuccessResponse<Token>() {
                    @Override
                    public void onSuccessWithObj(Token obj) {
                        super.onSuccessWithObj(obj);
                        prefUtil.put(AppConstant.PREF_TOKEN.AUTH_TOKEN, obj.getToken());
                        prefUtil.put(AppConstant.PREF_TOKEN.AUTH_USERNAME, username);
                        Toast.makeText(LoginActivity.this, "login success", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        finish();
                    }
                }, new OnErrorResponse() {
                    @Override
                    public void onError(int errorCode) {

                    }
                });
                break;
            case R.id.btn_register:
                startActivity(new Intent(this, RegisterActivity.class));
                break;
            default:
                break;
        }
    }
}